#pragma once
// mysql版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include "include/mysql.h"

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <cstdlib>
// 根据题库，加载所有题目信息到内存中
// model： 主要用来数据进行交互，对外提供访问的接口

namespace ns_model
{
    using namespace std;
    using namespace ns_util;
    using namespace ns_log;
    struct Question
    {
        std::string number; // 题目编号
        std::string title;  // 标题
        std::string star;   // 难度
        std::string desc;   // 描述
        std::string header; // 预设代码
        std::string tail;   // 测试用例，和header拼接完成完整代码
        int cpu_limit;      // 时间要求(s)
        int mem_limit;      // 空间要求(kb)
    };

    const std::string oj_questions = "oj_questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "123456";
    const std::string db = "oj";
    const int port = 3306;

    class Model
    {
    public:
        Model() {}
        bool QueryMysql(const std::string &sql, vector<Question> *out)
        {
            MYSQL *my = mysql_init(nullptr);
            if (nullptr == mysql_real_connect(my, host.c_str(), user.c_str(),
             passwd.c_str(),db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "连接数据库失败!"
                           << "\n";
                std::cout << mysql_errno(my)<<std::endl;
                return false;
            }
            mysql_set_character_set(my, "utf8mb4");
            LOG(INFO) << "连接数据库成功！"
                      << "\n";
            //执行sql语句
            if (0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARNING) << sql << " execute error!"
                             << "\n";
                return false;
            }
            // 提取结果
            MYSQL_RES *res = mysql_store_result(my);
            //分析结果
            int rows = mysql_num_rows(res);
            int cols = mysql_num_fields(res);
            struct Question q;
            for (int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit = atoi(row[7]);

                out->push_back(q);
            }
            // 释放结果空间
            free(res);
            mysql_close(my);
            return true;
        }
        bool GetAllQuestions(vector<Question> *out)
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMysql(sql, out);
        }
        bool GetOneQuestion(const string &number, Question *q)
        {
            bool res = false;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where id=";
            sql += number;
            vector<Question> result;
            if (QueryMysql(sql, &result))
            {
                if (result.size() == 1)
                {
                    *q = result[0];
                    res = true;
                }
            }
            return res;
        }
        ~Model() {}
    };
} // namespace ns_model
